package com.gelsanalyzer.ui.swing.command;

import java.util.*;

import javax.swing.*;

import com.gelsanalyzer.business.*;
import com.gelsanalyzer.business.control.*;
import com.gelsanalyzer.ui.swing.*;

public class DepartmentComboCommand extends JComboBox implements Command {
    // store a reference to sibling dependent control
    CourseNumComboCommand courseNumDropDown;
    // store a reference to the parent container
    CourseEntryPanel courseEntryPanel;
    // more dependencies. this one can probably be refactored out.
    Handler handler;

    public DepartmentComboCommand(Handler handler,
                                  CourseNumComboCommand courseNumDropDown) {
        super();
        this.handler = handler;
        this.courseNumDropDown = courseNumDropDown;
        this.addActionListener(handler);
        this.setDepartmentDropdownBox();
    }

    private void setDepartmentDropdownBox() {
        addDepartments( ((CourseEntryHandler) handler)
            .firePopulateDepartments());
    }

    // this guy populates the departments JComboBox with the database info...
    public void addDepartments(List<Department> findAllDepartments) {
        addItem(null);

        for ( Department department : findAllDepartments ) {
            addItem(department);
        }
    }

    public void Execute() {
        Department department = (Department) this.getSelectedItem();       
        if ( department != null ) {
            courseNumDropDown.addCourseNums( ((CourseEntryHandler) handler)
                .firePopulateCourses(this, department));
            
            courseNumDropDown.setEnabled(true);
        } else {
            ((CourseEntryHandler)handler).fireNullDepartmentSelected();        
    }
    }
}
